package cn.edu.sdjzu.xg.eagle.dao;




import cn.edu.sdjzu.xg.eagle.entity.Teacher;
import cn.edu.sdjzu.xg.eagle.util.Condition;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

public class TeacherDao {
    public static TeacherDao getInstance() {
        return null;
    }

    public Collection<Teacher> findAll(List<Condition> conditionList) throws SQLException {
        Collection<Teacher> desiredTeachers = new HashSet<>();
        StringBuilder sqlSelect = new StringBuilder("SELECT * FROM teacher");

        // 如果有条件，生成WHERE子句并附加到SQL语句后面
        if (conditionList != null && !conditionList.isEmpty()) {
            String whereClause = Condition.toWhereClause(conditionList);
            sqlSelect.append(whereClause);
        }

        Connection connection = JdbcHelper.getConn();
        PreparedStatement pstmtSelect = connection.prepareStatement(sqlSelect.toString());
        ResultSet resultSet = pstmtSelect.executeQuery();

        // 遍历结果集，创建Teacher对象并添加到集合中
        while (resultSet.next()) {
            Long id = resultSet.getLong("id");
            String name = resultSet.getString("name");
            int departmentId = resultSet.getInt("department_id");
            Teacher teacher = new Teacher(id, name, departmentId);
            desiredTeachers.add(teacher);
        }

        JdbcHelper.close(resultSet, pstmtSelect, connection);
        return desiredTeachers;
    }
}